/**
 * @file   test_mutex008.c
 * @brief  Test locks on different mutexes by the same task and the BRR should be restored to its original level.
 * Otherwise, BRR1 would never get to execute.
 * Expected result: 010101...
 */
#include "../header/common.h"
#include "../header/os.h"
#include "../header/uart.h"
#include "../header/trace.h"
#include <util/delay.h>
#ifdef test_mutex008

const unsigned char PPP[] = {};
const unsigned int PT = 0;
EVENT* print_event;
MUTEX* mutex1;
MUTEX* mutex2;

void another_task(void) {
	int arg = 0;
	arg = Task_GetArg();

    for(;;)
    {
        Mutex_Lock(mutex1, 1);
        add_to_trace(arg);
    	Mutex_Unlock(mutex1);
    	Task_Next();
    }
}

void generic_task(void)
{
	int arg = 0;
	arg = Task_GetArg();

    for(;;)
    {
        Mutex_Lock(mutex1, 1);
        Mutex_Lock(mutex2, 1);
        add_to_trace(arg);
    	Mutex_Unlock(mutex1);
    	Mutex_Unlock(mutex2);
    	Task_Next();
    }
}

int main(void) {
	//use pins 4,5,6 and 7 on Port D for output
	DDRD = (1<<PORTD4) | (1<<PORTD5) | (1<<PORTD6) | (1<<PORTD7);
	UART_BPS bitrate = UART_38400;
	uart_init(bitrate);

    print_event = Event_Init();
    mutex1 = Mutex_Init();
    mutex2 = Mutex_Init();

    Task_Create(generic_task, 0, BRR, 1);
    Task_Create(another_task, 1, BRR, 1);
    //Task_Create(system_task,1,SYSTEM,1);

    Event_Wait(print_event);
    print_trace();
    return 0;
}

#endif
